/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.config;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Fandy
 */
public class DatabaseConnection {
    private static Connection connection;
    private static Properties properties;
    private static InputStream fileConn;

    public static Connection getConnection() {
        try {
            fileConn = DatabaseConnection.class.getResourceAsStream("/com/fandi/config/db.properties");
            
            properties = new Properties();
            properties.load(fileConn);
            
            String driver   = properties.getProperty("driver");
            String dbname   = properties.getProperty("dbname");
            String url      = properties.getProperty("url") + dbname;
            String user     = properties.getProperty("user");
            String password = properties.getProperty("password");
            
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
            
        } catch (ClassNotFoundException ce) {
            JOptionPane.showMessageDialog(null, "Error : " + ce.getMessage());
        } catch (SQLException se) {
            JOptionPane.showMessageDialog(null, "Error : " + se.getMessage());
        } catch (IOException ie) {
            JOptionPane.showMessageDialog(null, "Error : " + ie.getMessage());
        }
        
        try {
            fileConn.close();
        } catch (IOException ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return connection;
    }
    
}
